<?php
/* 留言管理
 * 留言的几种情况（站内信息）
会员的留言 member_id !=0,      user_id = 0,    parent=0
公寓回复的留言 member_id =0,   user_id !=0,    parent !=0
公寓发送的消息 member_id = 0(公告)或!=0 user_id!=0, parent =0
会员回复公寓的消息member_id !=0 user_id =0 parent!=0
 * 公告信息暂不通过此作，而是在CMS中通过文章发布形式公布
 */

class CommentAction extends CommonAction {
    //TODO
    function _filter(&$map) {
        if(!empty($_REQUEST['keywords']) ){
            $key = trim($_REQUEST['keywords']);
            $member_id = M('Member')->where("name like '%".$key."%'")->field('id')->findAll();
             $houses = array();
             foreach ($member_id as $k => $value) {
                 $houses[$k] = $value['id'];
             }
             $member_id = implode(",", $houses);
             if(!empty($member_id)) {
                 $where['member_id'] = array('in',$member_id);
             }
            $where['content'] = array('like',"%".$key."%");
            $where['_logic'] = "or";
            $map['_complex'] = $where;
        }
        if(!empty($_REQUEST['member_id']) ){
            $map['member_id'] = $_REQUEST['member_id'];
        }
        if(!empty($_REQUEST['msgtype']) ){
            ($_REQUEST['msgtype'] == 0 )? ($map['user_id'] = 0):($map['user_id'] = array('neq',0) );
            //type=0会员留言,非0是公寓主动发送的消息
        }else{
            $map['user_id'] = 0;
        }
        $map['parent'] = 0;  //不显示后台回复信息

    }

    public function insert(){
        if(isset( $_REQUEST['member_id'] ) ){
            $email = empty($_REQUEST['email']) ? getModelName(intval($_REQUEST['member_id']),'Member','email')
                    : $_REQUEST['email'];
            $subject = "上海好家服务公寓的新信息！";
            $this->assign("user",getModelName(intval($_REQUEST['member_id']),"Member") );
            //Log::write("留言会员:".getModelName(intval($_REQUEST['member_id']),"Member") .' 会员ID'.$_REQUEST['member_id']);
            $this->assign("content",$_POST['content']);
           $content = $this->fetch('Email:reply');
           sendMail($subject, $content, '上海好家服务公寓<info@okhome.cn>', $email);
        }
        if(!empty($_REQUEST['parent']))
            updateLogStatus('comment',$_REQUEST['parent'],0);
        parent::insert();
        
    }

    
//TODO:留言的四个状态：后台已读，未读，前台已读，未读。
      public function _before_edit(){
          $this->assign('comstatus',C('comstatus'));
      }

      //查看给会员发送的信息的详情及会员的回复
      public function msgdetail(){
        $this->assign('vo',M('Comment')->where('id ='.$_GET['id'])->find() );
      }
}
?>